<?php
/**
 * 《帐号管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: Country.class.php 832 2010-01-06 09:17:14Z xuy $
 */
require_once(dirname(__FILE__) . '/WifiUtil.class.php');

class Connection extends WifiUtil {
	public $tbl = Constant::tbl_wifi_connection;

	public $status = array(
		'-' => '请选择',
		'1' => '正常',
		'2' => '暂停',
		'3' => '停止',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'uid' => '帐号',
		'ip' => 'IP',
		'mac' => 'MAC',
		'outgoing' => '上传',
		'incoming' => '下载',
		
		'bytes' => '流量',
		'speed' => '速度',

		'1' => '创建时间',
		'2' => '更新时间',
	);
	
	public $groupby = array(
		'-' => '请选择',
		'gw_id' => '节点',
		'uid' => '帐号',
		'ip' => 'ip',
		'mac' => 'mac',

		'status' => '状态',
		'1' => '小时',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('attribute_option', $this->attribute);
		$controller->assign_by_ref('op_option', $this->op);
	}

	function tab($controller, $request) {
		$data = array(
			'帐号管理' => array('cc'=>'radius.php?do=Radcheck.find', 'js'=>'1'),
			'认证信息' => array('cc'=>'radius.php?do=Radpost.find', 'js'=>'1'),
			'计费信息' => array('cc'=>'radius.php?do=Radacct.find', 'js'=>''),
		);
	
		$ret = $this->_tab($data);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'tab');
		return true;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
		
		$controller->assign('t1', time());

		$controller->display($request, 'wifi/connection/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}
		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($this->_audit('111')) {
			return true;
		}

		if ($row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}
	
	function _where($request) {
		$uid_s = $request['uid_s'];
		$name_s = $request['name_s'];
		
		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
		
		if (!empty($uid_s)) {
			$where .= " AND uid = '" . $uid_s . "'";
		}
		
		if (!empty($name_s)) {
			$where .= sprintf(" AND (gw_id LIKE '%s' OR ip LIKE '%s' OR mac LIKE '%s')", '%' . $name_s . '%', '%' . $name_s . '%', '%' . $name_s . '%');
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);
		
		return $where;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
	
		$emp = $_SESSION['id'];
	
		$p = array('1'=>'206', '2'=>'207', '3'=>'208');
	
		$a = $this->_audit_office($p);
		if (count($a) > 0) {
			$office = implode(",", $a);
	
			return sprintf(" (office in (%s) OR emp = '%s')", $office, $emp);
		}
	
		$dept = $this->_where_dept($emp);
		if (!empty($dept)) {
			return sprintf(" %s", $dept);
		}
	
		return sprintf(" emp = '%s'", $emp);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);
		
		$t1_s = $request['t1_s'];

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->_tbl($t1_s), $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$row['is_delete'] = $this->_is_delete($row);

				$data[$count] = $row;
				$count++;
			}

			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'wifi/connection/list');
		return true;
	}
	
	function _tbl($t1) {
		if (empty($t1)) {
			return $this->tbl;
		}
	
		$dd = date("Y-m-d");
	
		if ($t1 == $dd) {
			return $this->tbl;
		}
	
		$dd = date("Ymd", strtotime($t1));
		return sprintf("%s_%s", $this->tbl, $dd);
	}
	
	function black($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'wifi/connection/black');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'wifi/connection/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
	
		$row['mac_x'] = $this->_emp_by_mac($row['mac']);
		
		$row = $this->_view_t($row);
	
		return $row;
	}
	
	function group($controller, $request) {
		$this->pretreat_group($controller);
	
		$controller->assign('t1', time());
		// $controller->assign('t2', strtotime('+1 day'));
	
		$num = array(
			'1' => '按次数',
			'2' => '按流量',
		);
		$controller->assign('num_option', $num);
	
		$controller->display($request, 'wifi/connection/group');
		return true;
	}
	
	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'gw_id');
	
		$controller->assign('groupby', $this->groupby[$px]);
	
		return $this->groupby_dd($px);
	}
	
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);
		
		$t1_s = $request['t1_s'];
	
		$num_s = $request['num_s'];
		if (!empty($num_s) && $num_s == '2') {
			$fmt = "SELECT %s AS a, SUM(bytes) AS c FROM %s a ";
		} else {
			$fmt = "SELECT %s AS a, COUNT(bytes) AS c FROM %s a ";
		}
		$sql = sprintf($fmt, $this->s, $this->_tbl($t1_s));
	
		$where = $this->_where($request);
	
		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);
	
		$this->_group($controller, $request);
	
		// $controller->display($request, 'list');
		return true;
	}
	
	function _group($controller, $request) {
	
		$sql = $request['sql'];
	
		$this->_group2($controller, $sql);
	
		$tpl = $request['tpl'];
	
		if ($tpl == 'xls') {
			$filename = sprintf("%s.xls", date("YmdHis"));
			header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);
		}
	
		if ($tpl == 'doc') {
			$filename = sprintf("%s.doc", date("YmdHis"));
			header('Content-Disposition:application/msword; filename=' . $filename);
		}
	
		$view = $request['view'];
		if ($view == 'print') {
			$controller->display($request, 'list_2');
		} else {
			$controller->display($request, 'wifi/connection/group_list');
		}
	}
	
	function _name($row, $px) {
		if (empty($px) || $px == '-') {
			$px = 'gw_id';
		}
	
		$row = $this->_name_t($row, $px);
	
		return $row;
	}
	
	function _log() {
	}
	
	function backup($controller, $request) {
	
		$dd = date("Ymd", strtotime("-1 day"));
		$tbl = sprintf("%s_%s", $this->tbl, $dd);
	
		$db = newdb($this->dsn_w);
		$fmt = "ALTER TABLE %s RENAME TO %s";
		$sql = sprintf($fmt, $this->tbl, $tbl);
		$db->query($sql);
	
		$fmt = "CREATE TABLE %s LIKE %s";
		$sql = sprintf($fmt, $this->tbl, $tbl);
		$db->query($sql);
	
		$tbl = ltrim($this->tbl, 'wifi.');
		$tbl = sprintf("%s_%s", $tbl, date("Ymd", strtotime('-7 day')));
		$fmt = "DROP TABLE IF EXISTS `%s`";
		$sql = sprintf($fmt, $tbl);
		$db->query($sql);
	}
	
	function _new($dd, $node) {
	
		$ff = "DATE_FORMAT(createtime, '%Y-%m-%d')";
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT SUM(bytes) AS a, SUM(period) AS b FROM %s WHERE gw_id = '%s' AND %s = '%s'";
		$sql = sprintf($fmt, $this->_tbl($dd), $node, $ff, $dd);
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			return $row;
		}
	
		return false;
	}
	
	function t($controller, $request) {
		$this->pretreat($controller);

		echo substr("0755-89809672", 5);

		logdate($controller, $request);
		return true;
	}
}
?>